﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace TestTcpClient
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient tcpcl = new TcpClient();

            Thread.Sleep(10 * 1000);

            tcpcl.Connect(new IPEndPoint(IPAddress.Loopback, 5678));

            NetworkStream outs = tcpcl.GetStream();
            NetworkStream ins = tcpcl.GetStream();

            BinaryWriter bw = new BinaryWriter(outs);
            BinaryReader br = new BinaryReader(ins);

            int iOutMsg = 0;

            Thread.Sleep(60 * 1000);

            try
            {
                int count = 10;
                while (count-- > 0)
                {
                    bw.Write((iOutMsg++).ToString());

                    // string inMsg = br.ReadString();

                    // Console.WriteLine("Server returned: {0}", inMsg);
                }

                br.Close();
                bw.Close();
                tcpcl.Close();
            }
            catch (IOException)
            {
            }
            finally
            {
                br.Close();
                bw.Close();
                tcpcl.Close();
            }
        }
    }
}
